﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public enum PropType
{
    Life, weapon, Protect
}
public enum PropState
{
    Freedom,
    Follow,
    Dead
}
/// 道具的基类
public class Props : MonoBehaviour
{
    protected Vector3 dir;
    protected float speed = 1;
    protected float smooth = 5;
    protected float followRange = 1.5f;
    protected PropState state;
    protected Transform target;
    
    // Use this for initialization
    protected virtual void Start()
    {
        target = GameObject.FindGameObjectWithTag("Player").transform;
        dir = Random.insideUnitCircle;

        if (dir.y > 0) dir.y = -dir.y;

    }

    // Update is called once per frame
    protected virtual void Update()
    {
        switch (state)
        {
            case PropState.Freedom:
                Freedom();
                break;
            case PropState.Follow:
                Follow();
                break;
            default:
                break;
        }
    }
  


    protected virtual void Freedom()
    {
        if (transform.position.x <= -PlaneMove.MaxSize.x)
        {
            dir.x = -dir.x;
        }
        if (transform.position.x >= PlaneMove.MaxSize.x)
        {
            dir.x = -dir.x;
        }
        if (transform.position.y <= -PlaneMove.MaxSize.y)
        {
            dir.y = -dir.y;
        }
        if (transform.position.y >= PlaneMove.MaxSize.y)
        {
            dir.y = -dir.y;
        }
        transform.Translate(dir * speed * Time.deltaTime);
        if (Vector3.Distance(target.position, transform.position) <= followRange)
        {
            state = PropState.Follow;
        }
    }
    protected virtual void Follow()
    {
        transform.position = Vector3.Lerp(transform.position, target.position, smooth * Time.deltaTime);

        if (Vector3.Distance(target.position, transform.position) < 0.1f)
        {
            Attach();
        }
    }
    protected virtual void Attach()
    {
       
    }
}